package easy;

/**
 * 13. 罗马数字转整数
 * 创建时间：2024-03-19 11:29
 * 地址：https://leetcode.cn/problems/roman-to-integer/description/
 */
public class 罗马数字转整数_13 {
    public static int romanToInt(String s) {
        int f = value(s.charAt(0));
        int sum = 0;
        for(int i = 1; i < s.length(); i++) {
            int curr = value(s.charAt(i));
            if(f < curr) {
                sum -= f;
            } else {
                sum += f;
            }
            f = curr;
        }
        sum += f;
        return sum;
    }

    public static int value(char ch) {
        switch(ch) {
            case 'I': return 1;
            case 'V': return 5;
            case 'X': return 10;
            case 'L': return 50;
            case 'C': return 100;
            case 'D': return 500;
            case 'M': return 1000;
            default: return 0;
        }
    }

    public static void main(String[] args) {
        System.out.println(romanToInt("MCMXCIV"));
    }
}
